package com.zsgeek.wisdchain.qutu.base.customControl;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zsgeek.wisdchain.qutu.R;


/**
 * Item
 * Created by whb on 2018/1/22.
 */

public class ItemCheck extends RelativeLayout {

    private TextView mLeftTitleTV, mRightContentTV;
    private ImageView mLeftTitleIcon, mRightTitleIcon;

    private String titleText, contentText;
    private float titleTextSize = 0, contentTextSize = 0;   //sp
    private int leftIconId, rightIconId;


    public ItemCheck(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.item_check, this, true);
        mLeftTitleTV = findViewById(R.id.item_title);
        mLeftTitleIcon = findViewById(R.id.item_icon);
        mRightContentTV = findViewById(R.id.right_content_tv);
        mRightTitleIcon = findViewById(R.id.arrow_right_iv);
    }

    public ItemCheck(Context context, AttributeSet attrs) {
        super(context, attrs);
        //加载视图的布局
        LayoutInflater.from(context).inflate(R.layout.item_check, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemCheck);
        if (TextUtils.isEmpty(titleText)) {
            titleText = a.getString(R.styleable.ItemCheck_leftTitle);
        }
        if (TextUtils.isEmpty(contentText)) {
            contentText = a.getString(R.styleable.ItemCheck_rightContent);
        }
        if (titleTextSize == 0) {
            titleTextSize = a.getDimension(R.styleable.ItemCheck_leftTitleSize, 0);//获取左边字体大小
        }
        if (contentTextSize == 0) {
            contentTextSize = a.getDimension(R.styleable.ItemCheck_rightContentSize, 0);//获取到像素
        }
        if (leftIconId == 0) {
            leftIconId = a.getResourceId(R.styleable.ItemCheck_leftIcon, 0);
        }
        if (rightIconId == 0) {
            rightIconId = a.getResourceId(R.styleable.ItemCheck_rightIcon, 0);
        }

        a.recycle();
    }

    /**
     * 此方法会在所有的控件都从xml文件中加载完成后调用
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //获取子控件
        mLeftTitleTV = findViewById(R.id.item_title);
        mLeftTitleIcon = findViewById(R.id.item_icon);
        mRightContentTV = findViewById(R.id.right_content_tv);
        mLeftTitleTV.setText(titleText);
        mRightContentTV.setText(contentText);
        if (titleTextSize > 0)
            mLeftTitleTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleTextSize);
        if (contentTextSize > 0)
            mRightContentTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, contentTextSize);

        if (leftIconId <= 0) {
            mLeftTitleIcon.setVisibility(GONE);
        } else {
            mLeftTitleIcon.setImageResource(leftIconId);
        }
        if (rightIconId > 0) {
            mRightTitleIcon.setImageResource(rightIconId);
        }

    }

    //左边标题
    public void setLeftTitleName(String text) {
        if (mLeftTitleTV != null) {
            mLeftTitleTV.setText(text);
        }
        titleText = text;
    }

    //    右边内容显示文字
    public void setRightContentName(String text) {
        if (mRightContentTV != null) {
            mRightContentTV.setText(text);
        }
        contentText = text;
    }

    //    右边无内容隐藏
    public void setRightContentNoName() {
        if (mRightContentTV != null) {
            mRightContentTV.setVisibility(GONE);
        }
    }

    /**
     * 左边内容字体大小
     *
     * @param size 单位sp
     */
    public void setLeftTitleSize(float size) {
        if (mLeftTitleTV != null) {
            mLeftTitleTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
        titleTextSize = size;
    }

    /**
     * 右边内容字体大小
     *
     * @param size 单位sp
     */
    public void setRightContentNameSize(float size) {
        if (mRightContentTV != null) {
            mRightContentTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
        contentTextSize = size;
    }

    //    左边标题图片隐藏
    public void setLeftTitleNoIcon() {
        if (mLeftTitleIcon != null) {
            mLeftTitleIcon.setVisibility(GONE);
        }
    }

    //    左边标题图片显示
    public void setLeftTitleIcon(Drawable drawable) {
        if (mLeftTitleIcon != null) {
            mLeftTitleIcon.setImageDrawable(drawable);
        }
//        leftIconId = id;
    }

    //    左边标题图片隐藏
    public void setRightTitleIcon(int visible) {
        if (mRightTitleIcon != null) {
            mRightTitleIcon.setVisibility(visible);
        }
    }

}